
BEGIN { started = 0;

if( ARGC <=1 )
	{
	print("Please specify whether we are generating a template for UPPER or LOWER bound");
	exit(0);
	}
bound = ARGV[1];
ARGC=1;
}

(index($0, "representation") > 0) && (started==0) {
started = 1;
getline;	
# we've eaten "begin"
getline;
ROWS = $1;
COLS = $2;

numEqu = 0;

atRow = 1;
print("\\ Will be processing " ROWS " rows and " COLS " columns");
next;
}


(started==1) {
if( NF != COLS )
	{
	print(" Not correct number of columns in row " NR "...I counted " NF);
	exit(0);
	}

for( x=1; x<=COLS; x++ )
	{
	ineq[ atRow "." x ] = $x;
	}
atRow++;

if( atRow == (ROWS+1) )
	{
	getline;
	if( $0 == "end" )
		{
		if( bound == "UPPER" ) print("Maximize");
		else
		if( bound == "LOWER" ) print("Minimize");
		else	{
			print("The bound should be UPPER or LOWER. But I read, " bound ", exiting.");
			exit(0);
			}
		
		printf("obj: x!GETME!");

		printf("\n");

		print("Subject To");
	
		for(x=1; x<=ROWS; x++ )
			{
			seen = 0;

                        for( c=2; c<=COLS; c++ )
                                {
                                myval = ineq[ x "." c ];
                                if( c != 2 )
                                    {
                                    frontsym = substr(myval,1,1);
                                    if( (myval != "0") && (myval != "0.0") && (myval != "-0") && (myval != "-0.0") && (seen!=0) && (frontsym != "-")) printf(" + ");
                                    }
                                if( (myval != "0") && (myval != "0.0") && (myval != "-0") && (myval != "-0.0")  )
                                        {
                                        printf(" %sx%d", myval , c-1);
                                        seen = 1;
                                        }
                                }

                       	if( seen == 1 )
                                {
                               	printf(" >= ");

                                fchar = substr(ineq[x "." "1"],1,1);

                               	if( fchar == "-" ) print(substr(ineq[x "." "1"], 2));
                                else
                               	print("-" ineq[ x "." "1" ]);

                                # print((-1)*(ineq[ x "." "1" ]));
                               	}
                       	else
                               	{
                               	print(" 0x1 >= 0");
                               	}
			}

		print("Bounds");

		for(s=1; s<=(COLS-1); s++ )
			{
			printf(" -inf <= x%d <= +inf\n",s);
			}

		# print("General");
		print("End");

		exit(0);
		}
	else    {
		print("Expected an 'end' at line " atRow " , but got: " $0);
		}
	}
	else next;

}

